package com.daimajia.swipe.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.csh.szhm.adapter.MyBaseAdapter;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.interfaces.SwipeAdapterInterface;
import com.daimajia.swipe.implments.SwipeItemMangerImpl;
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface;

import java.util.List;

public abstract class BaseSwipeAdapter<T> extends MyBaseAdapter<T> implements
		SwipeItemMangerInterface, SwipeAdapterInterface {

	public BaseSwipeAdapter(Context c, String url) {
		super(c);
		deleteUrl = url;
		// TODO Auto-generated constructor stub
	}

	protected SwipeItemMangerImpl mItemManger = new SwipeItemMangerImpl(this);

	/**
	 * return the {@link com.daimajia.swipe.SwipeLayout} resource id, int the
	 * view item.
	 * 
	 * @param position
	 * @return
	 */
	public abstract int getSwipeLayoutResourceId(int position);

	/**
	 * generate a new view item. Never bind SwipeListener or fill values here,
	 * every item has a chance to fill value or bind listeners in fillValues. to
	 * fill it in {@code fillValues} method.
	 * 
	 * @param position
	 * @param parent
	 * @return
	 */
	public abstract View generateView(int position, ViewGroup parent);

	/**
	 * fill values or bind listeners to the view.
	 * 
	 * @param position
	 * @param convertView
	 */
	public abstract void fillValues(int position, View convertView);

	@Override
	public final View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			v = generateView(position, parent);
			if (!isAny) {
				mItemManger.initialize(v, position);
			}
		} else {
			if (!isAny) {
				mItemManger.updateConvertView(v, position);
			}
		}
		fillValues(position, v);
		return v;
	}

	private boolean isAny;

	public void setIsAny(boolean is) {
		isAny = is;
	}

	@Override
	public void openItem(int position) {
		mItemManger.openItem(position);
	}

	@Override
	public void closeItem(int position) {
		mItemManger.closeItem(position);
	}

	@Override
	public void closeAllExcept(SwipeLayout layout) {
		mItemManger.closeAllExcept(layout);
	}

	@Override
	public List<Integer> getOpenItems() {
		return mItemManger.getOpenItems();
	}

	@Override
	public List<SwipeLayout> getOpenLayouts() {
		return mItemManger.getOpenLayouts();
	}

	@Override
	public void removeShownLayouts(SwipeLayout layout) {
		mItemManger.removeShownLayouts(layout);
	}

	@Override
	public boolean isOpen(int position) {
		return mItemManger.isOpen(position);
	}

	@Override
	public SwipeItemMangerImpl.Mode getMode() {
		return mItemManger.getMode();
	}

	@Override
	public void setMode(SwipeItemMangerImpl.Mode mode) {
		mItemManger.setMode(mode);
	}

//	private HttpRequestTool<BaseModle> mHt;
//	private TypeToken<BaseModle> mTypeToken;
//
//	protected void deleteRequest(String id) {
//		((MyBaseActivity) mContext).shouCustomProgressDialog();
//		if (mHt == null) {
//			mHt = new HttpRequestTool<BaseModle>();
//			mTypeToken = new TypeToken<BaseModle>() {
//			};
//			mHt.setHttpCallBackUi(new HttpCallBackUi<BaseModle>() {
//
//				@Override
//				public void success(BaseModle data) {
//					((MyBaseActivity) mContext).dissmisCustomProgressDialog();
//					DebugUntil.createInstance().toastStr("删除成功~");
//					mData.remove(deletePosition);
//					notifyDataSetChanged();
//				}
//
//				@Override
//				public void requestType(String Type) {
//				}
//
//				@Override
//				public void failed(VolleyError error) {
//					((MyBaseActivity) mContext).error(error);
//				}
//			});
//		}
//		mHt.cloneRequest(Method.DELETE, deleteUrl + "/" + id, mTypeToken,
//				getClass().getSimpleName(), "DELETE");
//	}

	protected String deleteUrl = "";
	protected int deletePosition;
}
